package com.co.ud.icehome.connector;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Properties;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import com.co.ud.icehome.PanelSensorico;
import com.co.ud.icehome.sensores.TemperatureSensor;
import com.co.ud.icehome.utils.enums.KeyMessage;
import com.co.ud.icehome.utils.patterns.observer.Subject;

public class ClientMessageListener extends Observable implements
		javax.jms.MessageListener {

	private static ClientMessageListener clientMessageListener;
	private Map UsersConnected = new HashMap();
	private Logger logger = Logger.getLogger(this.getClass());
	private String messageGroup;
	private Subject controlLuminicoSession;


	public ClientMessageListener(PanelSensorico panel) {

		System.out.println("creando message listener");
		System.out.println("Asignando valores a reestablecer por ajaxpush " + panel + "...");
		this.setControlLuminicoSession(panel.getSubject());
		
		System.out.println("Asignando observador " + panel + "...");
		this.addObserver(panel);
		System.out.println("Observador asignado OK");

		// message group
		this.messageGroup = "" + panel.getMessageGroup();

		Properties props = new Properties();
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
		props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
		Context ctx;
		try {
			ctx = new InitialContext(props);
			ConnectionFactory queueConnectionFactory = (ConnectionFactory) ctx
					.lookup("ConnectionFactory");

			Destination myQueue = (Destination) ctx
					.lookup("dynamicQueues/IceHomeReceiveQueue");
			Connection connection = queueConnectionFactory.createConnection();

			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);

			MessageConsumer messageConsumer = session.createConsumer(myQueue,
					"JMSXGroupID='" + this.messageGroup + "'");
			messageConsumer.setMessageListener(this);
			// start the connection
			connection.start();

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void onMessage(Message arg0) {
		// TODO Auto-generated method stub
		System.out.println("on message!!!!!");
		System.out.println(System.currentTimeMillis());
		Subject subject = new Subject();
		

		TextMessage msg = (TextMessage) arg0;

		String[] info = null;
		try {
			info = msg.getText().split(KeyMessage.SEPARATOR.getMsg());

			if (info != null) {
				subject.setTemperatura("" + info[1]);

				System.out.println("Notificando a observadores...");
				setChanged();
				this.notifyObservers(subject);
				System.out.println("Notificando a observadores OK");
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Map getUsersConnected() {
		return UsersConnected;
	}

	public void setUsersConnected(Map usersConnected) {
		UsersConnected = usersConnected;
	}
	
	public Subject getControlLuminicoSession() {
		return controlLuminicoSession;
	}

	public void setControlLuminicoSession(Subject controlLuminicoSession) {
		this.controlLuminicoSession = controlLuminicoSession;
	}

}